package com.bk.bands;

import java.io.InputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.PropertyException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.ValidationEvent;
import javax.xml.bind.ValidationEventHandler;

import android.widget.LinearLayout;
import com.bk.BKActivity;
import com.bk.R;
import com.bk.bands.common.Dimension;
import com.bk.bands.evaluate.Evaluator;
import com.bk.bands.evaluate.ValueIterator;
import com.bk.bands.model.Template;
import com.bk.bands.runtime.BandsException;
import com.bk.bands.template.XmlBand;
import com.bk.bands.template.XmlBorder;
import com.bk.bands.template.XmlFields;
import com.bk.bands.template.XmlFont;
import com.bk.bands.template.XmlPosition;
import com.bk.bands.template.XmlShape;
import com.bk.bands.template.XmlStyle;
import com.bk.bands.template.XmlStyles;
import com.bk.bands.template.XmlTemplate;
import com.bk.bands.template.XmlTemplateFactory;
import com.bk.bands.template.XmlText;
import com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl;

/**
 * Created by IntelliJ IDEA.
 * User: SG0891787
 * Date: 3/5/12
 * Time: 8:45 PM
 */
public class TestBands extends BKActivity
{

   protected void onCreate(android.os.Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);

      try
      {
         XmlTemplateFactory templateFactory = new XmlTemplateFactory(new XmlTemplate());

         Class[] classesToBound = new Class[]
            {
               XmlStyle.class,
               XmlBorder.class,
               XmlStyles.class,
               XmlFont.class,
               XmlText.class,
               XmlShape.class,
               XmlFields.class,
               XmlBand.class,
               XmlTemplate.class,
               XmlPosition.class
            };


         JAXBContext jaxbContext = JAXBContext.newInstance(classesToBound);
         Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
         unmarshaller.setProperty(UnmarshallerImpl.FACTORY,templateFactory);

         InputStream templateFile = getResources().openRawResource(R.raw.faktura_rr);


         unmarshaller.setEventHandler(new ValidationEventHandler()
         {
            public boolean handleEvent(ValidationEvent event)
            {
               return false;
            }
         });

         final Evaluator headerEvaluator = new Evaluator()
         {
            public String evaluate(String valueExpression)
            {
               return "HEADER";
            }
         };

         final Evaluator footerEvaluator = new Evaluator()
         {
            public String evaluate(String valueExpression)
            {
               return "FOOTER";
            }
         };

         XmlTemplate xmlTemplate = (XmlTemplate) unmarshaller.unmarshal(templateFile);
         Template template = xmlTemplate.getTemplate();

         DataSource ds = new DataSource()
         {
            public Evaluator getHeaderValues()
            {
               return headerEvaluator;
            }

            public Evaluator getFooterValues()
            {
               return footerEvaluator;
            }

            public ValueIterator getBandValues(String bandName)
            {

               return new ValueIterator()
               {
                   int i = 0;
                  public boolean next()
                  {

                     return i++ < 10;
                  }

                  public String evaluate(String valueExpression)
                  {
                     return "FIELD" + String.valueOf(i);
                  }
               };
            }
         };

         Document document = Document.fill(template, ds);
         BitmapPaper paper = new BitmapPaper(200,new Dimension(document.getWidth(),document.getHeight()));

         document.print(paper);


         setContentView(new PaperView(this,paper),new  LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT));
      }
      catch (BandsException e)
      {
         System.out.println(e);
      }
      catch (PropertyException e)
      {
         System.out.println(e);
      }
      catch (JAXBException e)
      {
         System.out.println(e);
      }



   }



}
